package com.honghu.villagepost.dialog;

import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.method.ScrollingMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.StyleSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.ada.common.util.CollectionUtil;
import com.blankj.utilcode.util.ToastUtils;
import com.honghu.villagepost.R;
import com.honghu.villagepost.activity.AddrActivity;
import com.honghu.villagepost.activity.LogisticsOrderDetailsActivity;
import com.honghu.villagepost.activity.WebViewActivity;
import com.honghu.villagepost.adapter.MarqueeViewAdapter;
import com.honghu.villagepost.api.ApiService;
import com.honghu.villagepost.callback.DialogCallback;
import com.honghu.villagepost.callback.LzyResponse;
import com.honghu.villagepost.constant.Constants;
import com.honghu.villagepost.entity.Addr;
import com.honghu.villagepost.entity.request.LogisticsOrderReq;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.Map;


/**
 * Desc:同城代寄-弹框
 * <p>
 * Author: [李豫]
 * Date: 2018-08-02
 * Copyright: Copyright (c) 2010-2018
 * Updater:
 * Update Time:
 * Update Comments:
 */
public class AgreementDialog extends DialogFragment implements View.OnClickListener {

    private TextView mTvContent;

    private AgreementDialogListener mListener;

    public void setAgreementDialogListener(AgreementDialogListener listener) {
        this.mListener = listener;
    }

    public static AgreementDialog newInstance() {
        AgreementDialog payMethodDialog = new AgreementDialog();
        return payMethodDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(true);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.customDialog);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().setCanceledOnTouchOutside(false);
        setCancelable(false);
        Window window = getDialog().getWindow();
        window.setGravity(Gravity.CENTER);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View dialogView = inflater.inflate(R.layout.dialog_agreement, container, false);

        mTvContent = dialogView.findViewById(R.id.tv_content);
        dialogView.findViewById(R.id.tv_agree).setOnClickListener(this);
        dialogView.findViewById(R.id.tv_not_agree).setOnClickListener(this);

        initData();
        return dialogView;
    }

    private void initData() {
        String argreement_1 = getContext().getString(R.string.agreement_1);
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(argreement_1);

        Spannable spannableString = new SpannableString("《服务协议》");
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                WebViewActivity.start(getActivity(), "服务协议", Constants.SERVICE_PROTOCOL);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(getContext().getResources().getColor(R.color.color_FF8C4D));
                ds.setUnderlineText(false); //去掉下划线
            }
        };
        spannableString.setSpan(clickableSpan,0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.append(spannableString);

         String argreement_2 = getContext().getString(R.string.agreement_2);
         spannableStringBuilder.append(argreement_2);

        spannableStringBuilder.append(getBoldText("(1)位置权限：")).append("基于位置信息为您提供产品查询、个性化商品推荐服务以及更优的用户体验\n");
        spannableStringBuilder.append(getBoldText("(2)设备信息权限：")).append("为您提供账号相关的安全服务，会申请读取手机设备表示(IMEI)及MAC地址等信息，用户关联您的账号\n");
        spannableStringBuilder.append(getBoldText("(3)存储权限：")).append("正常启动APP、保存或读取照片、媒体内容等，用于您修改头像等功能\n");

         String argreement_3 = getContext().getString(R.string.agreement_3);
        spannableStringBuilder.append(argreement_3);

        Spannable spannablePrivacy = new SpannableString("《隐私政策》");
        ClickableSpan Privacy = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                WebViewActivity.start(getActivity(), "隐私政策", Constants.PRIVATE_PROTOCOL);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(getContext().getResources().getColor(R.color.color_FF8C4D));
                ds.setUnderlineText(false); //去掉下划线
            }
        };
        spannablePrivacy.setSpan(Privacy,0, spannablePrivacy.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.append(spannablePrivacy);

        String argreement_4 = getContext().getString(R.string.agreement_4);
        spannableStringBuilder.append(argreement_4);

        mTvContent.setMovementMethod(ScrollingMovementMethod.getInstance());
        mTvContent.setMovementMethod(LinkMovementMethod.getInstance());
        mTvContent.setText(spannableStringBuilder);

    }

    /**
     * Desc:获取粗体内容
     * <p>
     * Author: [李豫]
     * Date: 2021-05-29
     *
     * @param content
     * @return spannable string
     */
    private SpannableString getBoldText(String content) {
        SpannableString spanString = new SpannableString(content);
        StyleSpan span = new StyleSpan(Typeface.BOLD);
        spanString.setSpan(span, 0, content.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanString;
    }


    public void show(FragmentManager manager) {
        if (!isShowing()) {
            show(manager, AgreementDialog.class.getSimpleName());
        }
    }

    public boolean isShowing() {
        if (getDialog() != null && getDialog().isShowing()) {
            return true;
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        if (viewId == R.id.tv_agree) {
            if (mListener != null){
                mListener.onAgree();
            }
        } else if (viewId == R.id.tv_not_agree) {
            if (mListener != null){
                mListener.onDisAgree();
            }
        }
    }

    public interface AgreementDialogListener{
        void onAgree();
        void onDisAgree();
    }
}

